/*
  Copyright (C) 2023 mio <stigma@disroot.org>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "common.h"
#include "ar.h"
#include "extractor-gui.h"

#include <tqapplication.h>
#include <tqdir.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqprogressbar.h>
#include <tqthread.h>
#include <tqurl.h>

constexpr int EX_EVENT_EXTRACT_COMPLETE = TQEvent::User + 1;

class ExWindow : public TQWidget
{
    TQ_OBJECT

public:
    ExWindow(const TQString& source)
        : TQWidget(nullptr, nullptr)
    {
        TQString path = TQDir::cleanDirPath(source);
        TQUrl url(path);

        setCaption(TQString::fromLatin1("ex - Extracting %1").arg(url.fileName()));

        auto layout = new TQVBoxLayout(this, 4, 4);
        layout->addStretch(1);

        m_label = new TQLabel(TQString::fromLatin1("Extracting %1...").arg(source), this);
        layout->addWidget(m_label);

        // Would be nice if TQProgressBar indeterminate didn't need the timer...
        m_bar = new TQProgressBar(0, this);
        layout->addWidget(m_bar);

        layout->addStretch(1);

        resize(minimumSize());

        m_timerId = startTimer(10);
    }

protected:
    void customEvent(TQCustomEvent *ev) override
    {
        if (ev->type() == EX_EVENT_EXTRACT_COMPLETE) {
            m_label->setText("Extracting complete!");
            m_bar->setTotalSteps(1);
            m_bar->setProgress(1);
            killTimer(m_timerId);
        }
    }

    void timerEvent(TQTimerEvent *ev) override
    {
        if (ev->timerId() == m_timerId) {
            m_bar->setProgress(m_bar->progress() + 1);
        }
    }

private:
    TQLabel *m_label;
    TQProgressBar *m_bar;
    int m_timerId;
};

struct tqt_context
{
    TQApplication *app;
    ExWindow *window;
    AR *ar;
};

static tqt_context gContext;

class ExThread : public TQThread
{
protected:
    void run()
    {
        ar_decompress_archive(gContext.ar);
        ar_end_extracting(gContext.ar);
        gContext.ar = nullptr;

        auto event = new TQCustomEvent(EX_EVENT_EXTRACT_COMPLETE);
        TQApplication::postEvent(tqApp->mainWidget(), event);
    }
};

bool extractor_gui_init(const char *source, const char *dest, const AROptions *options)
{
    int argc = 0;
    char *argv[] = { nullptr };

    // Allocate so we don't loose this to stack
    gContext.app = new TQApplication(argc, argv);
    gContext.window = new ExWindow(source);

    gContext.app->setMainWidget(gContext.window);

    gContext.ar = ar_init_extractor(source, dest, options);

    return true;
}

void extractor_gui_run()
{
    ExThread thread;
    thread.start();

    gContext.window->show();
    gContext.app->exec();

    thread.terminate();
    thread.wait();
}

void extractor_gui_deinit()
{
    if (gContext.ar) {
        // Window closed before extraction finished == thread terminated.
        ar_end_extracting(gContext.ar);
    }

    delete gContext.window;
    delete gContext.app;
}

#include "extractor-tqt.moc"
